package com.laity.add;

import java.util.concurrent.CountDownLatch;

/**
 * @author: Laity
 * @Project: JavaLaity
 * @Package: com.laity.add.CountDownLatchDemo
 * @Date: 2023年11月26日 22:49
 * @Description: 计数器
 */

public class CountDownLatchDemo {

    public static void main(String[] args) {
        // 总数为6，必须要执行任务的时候，再使用
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                countDownLatch.countDown(); // 计数器减1
                System.out.println(Thread.currentThread().getName() + "走了");
            }, "thread-" + i).start();
        }
        try {
            countDownLatch.await();
            System.out.println("所有线程都走了");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
